#!/usr/bin/env python

import wsgiref.handlers
from google.appengine.ext import db
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
import datetime
from django.utils import simplejson
import cgi

from model import Topic
from model import Question
from model import ClassState


class MyHandler(webapp.RequestHandler):
    
    def get(self):

        topics = db.Query(Topic)
        db.delete(topics)

        form = cgi.FieldStorage()

        classCount = 0

        while (form.getvalue("topicname" + str(classCount)) != None):
            name = form.getvalue("topicname" + str(classCount))
            time = 20 
                #int(form.getvalue("topictime" + str(classCount)))
            t = Topic(index=classCount, time=time, name=name)
            t.put()
            classCount = classCount + 1
        

        # removed so you can add questions individually
        # questionCount = 0
        # questions = db.Query(Question)
        # db.delete(questions)

        # while (form.getvalue("questiontext" + str(questionCount)) != None):
        #     text = form.getvalue("questiontext" + str(questionCount))

        #     answer1 = form.getvalue("answer1_" + str(questionCount))
        #     answer2 = form.getvalue("answer2_" + str(questionCount))
        #     answer3 = form.getvalue("answer3_" + str(questionCount))
        #     answer4 = form.getvalue("answer4_" + str(questionCount))

        #     correctanswer = int(form.getvalue("correctanswer" + str(questionCount)))

        #     answerarr = [answer1, answer2, answer3, answer4]
        #     q = Question(text=text, answers=answerarr, correctAnswer=correctanswer)
        #     q.put()
        #     questionCount = questionCount + 1

        
        classState=ClassState(currentSubjectIndex=0,isQuizOn=False)
        classState.put();

def main():
    app = webapp.WSGIApplication([
        (r'.*', MyHandler)], debug=True)
    wsgiref.handlers.CGIHandler().run(app)

if __name__ == "__main__":
    main()